from timescale.db.models.models import TimescaleModel, TimescaleDateTimeField

from django.db import models
from django.db.models.fields.json import JSONField


__all__ = (
    'SimpleTable',
)


class Entity(models.Model):
    name = models.CharField(max_length=16)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=('name',), name='%(app_label)s__entity__name')
        ]


class SimpleTable(TimescaleModel):
    time = TimescaleDateTimeField(interval='1 min')
    entity = models.ForeignKey(Entity, on_delete=models.PROTECT)
    data = JSONField(default=dict)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=('time', 'entity',), name='%(app_label)s__simpletable__time__entity')
        ]
